define(function(require,exports,module) {
    // require('./plug/hammer.min');
    // require('./plug/lazyload_echo');
	var myGrid = require('./plug/minigrid');
    var myPub = require('./public');

    var GridList = $('.js_gridList'); 		//单个产品

    var oMoreBtn = $('.js_moreBtn');
    var realHeight = "";
    var realWidth = "";
    var imgUrl="";

    module.exports = {
        init:function(params1,params2){
        	httpImageUrl = params1;
        	categoryId =  params2;
        }
    };

    function setImgHeight(obj,over) {
    	var _aImgHeight_start = []; 	//原始高度
    	var _aImgHeight_end = [];		//最后高度
    	var _aImgWidth_start = [];		//原始宽度
    	var _aImgWidth_end = GridList.width();//最后宽度

    	//if(before)before()
    	/*obj.find('img').each(function(i) {

    		var imgUrl = $("#flowerImage_"+i).attr('data-echo');
    		var img = new Image();
    		img.src = imgUrl;
    		img.onload = function(){
    			realHeight = img.height;
    			//alert(realHeight);
    			_aImgHeight_start[i] = obj.find('img').eq(i).attr('protoHeight',realHeight);
    			//alert(realHeight);
    			_aImgWidth_start[i] = obj.find('img').eq(i).attr('protoWidth');
    			_aImgHeight_end[i] = _aImgHeight_start[i] * _aImgWidth_end / _aImgWidth_start[i];
    			obj.find('img').eq(i).parents('.js_imgBox').css({'height': _aImgHeight_end[i] });
    		}
    	})*/
    	obj.find('img').each(function(i) {
			_aImgHeight_start[i] = obj.find('img').eq(i).attr('protoHeight');
			_aImgWidth_start[i] = obj.find('img').eq(i).attr('protoWidth');
			_aImgHeight_end[i] = _aImgHeight_start[i] * _aImgWidth_end / _aImgWidth_start[i];
			obj.find('img').eq(i).parents('.js_imgBox').css({'height': _aImgHeight_end[i] });
    	})
    	if(over)over()
 	}




    myGrid.MiniGrid('.js_grid', '.js_gridList',function() {
    	setImgHeight(GridList);
    });




    $(window).on('resize', function() {
        myGrid.MiniGrid('.js_grid', '.js_gridList',function() {
	    	setImgHeight(GridList);
	    });
    });

    //滚动加载开关
    var scrollFlag = "";
    $(window).on('scroll',function() {
    	myPub.throttle(function() {
    		if(scrollFlag){
    			//ajax返回值flag为true时 滚动加载scrollflag也为true 此时关闭滚动加载
    			return;
    		}else{
    			ScrollLoading(100);
    		}
    	},window)
    	//console.log($(window)[0].clientHeight)
    	//console.log($(document).scrollHeight())



    })

    myPub.lazLoadingImgEcho();



  /*  var testHtml = '<div class="gridList js_gridList">'+
                		'<a href="goods_detail.html" >'+
	                		'<figure class="js_imgBox VBox BoxCenter">'+
		                		'<img  src="/static_v3/image/loadImg.jpg" data-echo="/static_v3/image/flower5.jpg" protoHeight="290" protoWidth="290" />'+
		                	'</figure>'+

		                	'<aside>'+
		                		'<h5>一束法国娇艳红玫瑰，让 你的双十一不再孤单</h5>'+
		                		'<p>为世人称道的法国皇家红玫 瑰配上幽兰草 暖暖心意为世人称道的法国皇家红玫 瑰配上幽兰草 暖暖</p>'+
		                	'</aside>'+
                		'</a>'+
                	'</div>'
    */




    var start=2;
    //滚动距离页面多少距离开始加载
    var html = "";
    var flag = "";//ajax返回值
    function getFlowerList(){

    	  $.ajax({
			url:'/shop/goods/flowerListByPage',
			type:'post',
			data:{start:start,limit:8,categoryId:categoryId},
			async: false,
			dateType:"json",
			success:function(data){
				if(data!=null){
					var goodsList = eval(data);
					//查询结果小于5个 将flag改为true
					if(goodsList.length <8){
						flag = true;
					}

					if(goodsList.length == 0){
						//查询结果没有时  flag为ture 用于禁止滚动加载
						flag = true;
					}else{
						//$('#listAppend_'+(page)+'').remove();
						//alert(goodsList);

						$.each(goodsList,function(index,obj){

							imgUrl = httpImageUrl+obj.flowerImagePath ;
							html ='<div class="gridList js_gridList">'+
	                		'<a href="/shop/goods/view/'+obj.goodsSn+'" >'+
		                		'<figure class="js_imgBox VBox BoxCenter">'+
			                		'<img  src="/static_v3/image/loadImg.jpg" data-echo="'+httpImageUrl+obj.flowerImagePath+'" protoHeight="'+obj.flowerImageHeight
			                		+'" protoWidth="'+obj.flowerImageWidth+'" />'+
			                	'</figure>'+

			                	'<aside>'+
			                		'<h5>'+obj.name+'</h5>'+
			                		'<p>'+getTitle(obj.title)+'</p>'+
			                	'</aside>'+
	                		'</a>'+
	                	'</div>';

							$('.js_grid').append(html);
						});

						start++;
						myPub.lazLoadingImgEcho();
			    		myGrid.MiniGrid('.js_grid', '.js_gridList', function(){
			    			setImgHeight($('.js_gridList'));
			    		});
					}
				}else{
					//查询的data为空时flag为true
					flag = true;
				}
			}
		});

    }

    function ScrollLoading(minHeight) {
    	if($(document).height() - $(window).scrollTop() - $(window).height() <= minHeight) {

//    		alert(minHeight);
    		//ajax加载数据前执行

			//oMoreBtn.hide();	//隐藏 没有更多

			//滚动加载
    		getFlowerList();

    		if(flag){
    			//滚动加载开关
    			scrollFlag = true;
    		}





    		//ajax成功后执行
    		myPub.EchoRush();
    		myGrid.MiniGrid('.js_grid', '.js_gridList', function(){
    			($('.js_gridList'));
    		});

    		//ajax没有数据后执行

			//oMoreBtn.show();  //没有更多


    	}
    }

    function getTitle(obj){
		if(obj == null){
			return null;
		}else if(obj == 'undefined'){
			return null;
		}else{
			return obj;
		}

	}




    /*$('.js_jiazai').click(function() {
    	$('.js_grid').append($('.js_grid').html())
    	myGrid.MiniGrid('.js_grid', '.js_gridList');
    })*/

});